##############################
##     run bootstrap on     ##
##  cleaned cces and output ##
## matrices of distribution ##
##       of opinion by      ##
##        party/class       ##
##############################

rm(list = ls())

# setwd('~/../../Class+Party/')

.libPaths('../rpackages')

library(tidyverse)

args <- commandArgs(TRUE)
print(args)

sim <- as.numeric(args[1])

source('bootstrap_funs.R')

options(dplyr.summarise.inform = FALSE)

cces_allyears <- readRDS('cces_allyears.rds') %>% 
  filter(year != 2010)

cces_allyears <- cces_allyears %>% 
  subset_unique_questions()
cces_allyears <- cces_allyears %>%   # drops leaners and weird cross-party identifiers from being d or r
  mutate(pid3 = case_when(pid7 %in% c('Lean Democrat', 'Strong Democrat', 'Not very strong Democrat') ~ 'Democrat',
                          pid7 %in% c('Lean Republican', 'Strong Republican', 'Not very strong Republican') ~ 'Republican',
                          TRUE ~ pid3))

issuetopics <- cces_allyears %>% 
  select(question, year, pap_topic, topic_3, topic_6) %>% 
  distinct() %>% 
  arrange(question)

parties <- c('dem', 'rep', 'all')

ptm <- proc.time()

output <- list()

for (s in c(sim*2, (sim*2)+1)) {
  set.seed(s)
  
  boot_cces <- boot_sample_cces(cces_allyears, quantile = 0.1)
  
  boot_cces$pid_new <- NA
  boot_cces$pid_new[boot_cces$pid3 %in% c('Republican', 'Democrat')] <- boot_cces$pid3[boot_cces$pid3 %in% c('Republican', 'Democrat')]
  boot_cces$pid_new[boot_cces$pid3 %in% c('Independent', 'Other')] <- 'Independent/Other'
  boot_cces <- boot_cces[,names(boot_cces) != 'pid3']
  
  boot_cces <- boot_cces %>% 
    drop_na(opinion)
  
  # class by urban analysis
  
  boot_cces$faminc_group <- NA
  boot_cces$faminc_group[boot_cces$faminc_bottom == 1] <- 'bottom'
  boot_cces$faminc_group[boot_cces$faminc_top == 1] <- 'top'
  boot_cces$faminc_group[boot_cces$faminc_middle == 1] <- 'middle'
  
  
  group_opinions <- boot_cces %>%
    filter(pid_new != 'Independent/Other') %>% 
    drop_na(pid_new, faminc_group, urbanrural) %>% 
    group_by(pid_new, faminc_group, urbanrural, question) %>% 
    summarize(opinion = sum(opinion) / n()) %>% 
    ungroup() %>% 
    bind_rows(boot_cces %>%
                drop_na(faminc_group, urbanrural) %>% 
                group_by(faminc_group, urbanrural, question) %>% 
                summarize(opinion = sum(opinion) / n()) %>% 
                mutate(pid_new = 'All') %>% 
                ungroup()) %>% 
    mutate(party_income = paste(pid_new, faminc_group, urbanrural, sep = '_')) %>% 
    select(-pid_new, -faminc_group, -urbanrural) %>% 
    spread(key = party_income, value = opinion) %>% 
    arrange(question) %>% 
    column_to_rownames('question')
  
  for (p in c('all', 'dem', 'rep')) {
    pname <- case_when(p == 'all' ~ 'All',
                       p == 'dem' ~ 'Democrat',
                       p == 'rep' ~ 'Republican')
    
    output[[paste(p, 'inctop_urban', '10', 'sim', s, sep = '_')]] <-  group_opinions[,paste(pname, 'top_urban', sep = '_')]
    output[[paste(p, 'incbottom_urban', '10', 'sim', s, sep = '_')]] <-  group_opinions[,paste(pname, 'bottom_urban', sep = '_')]
    output[[paste(p, 'inctop_rural', '10', 'sim', s, sep = '_')]] <-  group_opinions[,paste(pname, 'top_rural', sep = '_')]
    output[[paste(p, 'incbottom_rural', '10', 'sim', s, sep = '_')]] <-  group_opinions[,paste(pname, 'bottom_rural', sep = '_')]
  }
  
}

print((proc.time() - ptm)/60)

saveRDS(output,
        paste0('bootstrap_out/classbyurban/classbyurban_boot_', sim, '.RDS'))
